package com.jhgsys.internal.hazard.controller;

import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.entity.HazardReportFile;
import com.jhgsys.internal.hazard.service.IHazardReportFileService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 隐患报告附件 Controller
 *
 * @author wanghw
 * @date 2024-08-20 09:23:07
 */
@Slf4j
@Validated
@Controller
public class HazardReportFileController extends BaseController {

    @Autowired
    private IHazardReportFileService hazardReportFileService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportFile")
    @RequiresPermissions("hazardReportFile:view")
    public String hazardReportFileIndex(){
        return FebsUtil.view("hazard/hazardReportFile/hazardReportFileList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportFile/add")
    @RequiresPermissions("hazardReportFile:add")
    public String hazardReportFileAdd(){
        return FebsUtil.view("hazard/hazardReportFile/hazardReportFileEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardReportFile/update/{id}")
    @RequiresPermissions("hazardReportFile:edit")
    public String hazardReportFileUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("hazardReportFile", hazardReportFileService.findHazardReportFileById(id));
        return FebsUtil.view("hazard/hazardReportFile/hazardReportFileEdit");
    }

    /**
     * 查询（所有）
     * @author wanghw
     * @date 2024-08-20 09:23:07
     */
    @GetMapping("hazardReportFile")
    @ResponseBody
    @RequiresPermissions("hazardReportFile:view")
    public FebsResponse getAllHazardReportFiles(HazardReportFile hazardReportFile) {
        return new FebsResponse().success().data(hazardReportFileService.findHazardReportFileList(hazardReportFile));
    }

    /**
     * 查询（分页）
     * @author wanghw
     * @date 2024-08-20 09:23:07
     */
    @GetMapping("hazardReportFile/list")
    @ResponseBody
    @RequiresPermissions("hazardReportFile:view")
    public FebsResponse hazardReportFileList(QueryRequest request, HazardReportFile hazardReportFile) {
        Map<String, Object> dataTable = getDataTable(this.hazardReportFileService.findHazardReportFilePage(request, hazardReportFile));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 HazardReportFile
     * @author wanghw
     * @date 2024-08-20 09:23:07
     */
    @Log("新增HazardReportFile")
    @PostMapping("hazardReportFile")
    @ResponseBody
    @RequiresPermissions("hazardReportFile:add")
    public FebsResponse addHazardReportFile(@Valid HazardReportFile hazardReportFile) throws FebsException {
        try {
            hazardReportFile.setCreatedBy(getCurrentUser().getUserName());
            this.hazardReportFileService.createHazardReportFile(hazardReportFile);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardReportFile失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 HazardReportFile
     * @author wanghw
     * @date 2024-08-20 09:23:07
     */
    @Log("逻辑删除HazardReportFile")
    @GetMapping("hazardReportFile/delete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardReportFile:delete")
    public FebsResponse deleteHazardReportFile(@PathVariable("id") String hazardReportFileIds) throws FebsException {
        try {
            HazardReportFile hazardReportFile = new HazardReportFile();
            hazardReportFile.setDeletedBy(getCurrentUser().getUserName());
            hazardReportFile.setDeletedTime(new Date());
            hazardReportFile.setDeletedFlg(FebsConstant.INVALID);
            this.hazardReportFileService.deleteHazardReportFile(hazardReportFile,hazardReportFileIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardReportFile失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 HazardReportFile
    * @author wanghw
    * @date 2024-08-20 09:23:07
    */
    @Log("物理删除HazardReportFile")
    @GetMapping("hazardReportFile/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardReportFile:delete")
    public FebsResponse physicallyDeleteHazardReportFile(@PathVariable("id") String hazardReportFileIds) throws FebsException {
        try {
            this.hazardReportFileService.physicallyDeleteHazardReportFile(hazardReportFileIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardReportFile失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 HazardReportFile
     * @author wanghw
     * @date 2024-08-20 09:23:07
     */
    @Log("修改HazardReportFile")
    @PostMapping("hazardReportFile/update")
    @ResponseBody
    @RequiresPermissions("hazardReportFile:edit")
    public FebsResponse updateHazardReportFile(HazardReportFile hazardReportFile) throws FebsException {
        try {
            hazardReportFile.setUpdatedBy(getCurrentUser().getUserName());
            this.hazardReportFileService.updateHazardReportFile(hazardReportFile);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardReportFile失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 导出 HazardReportFile的excel
     * @author wanghw
     * @date 2024-08-20 09:23:07
     */
    @PostMapping("hazardReportFile/excel")
    @ResponseBody
    @RequiresPermissions("hazardReportFile:view")
    public void export(QueryRequest queryRequest, HazardReportFile hazardReportFile, HttpServletResponse response) throws FebsException {
        try {
            List<HazardReportFile> hazardReportFiles = this.hazardReportFileService.findHazardReportFilePage(queryRequest, hazardReportFile).getRecords();
            ExcelKit.$Export(HazardReportFile.class, response).downXlsx(hazardReportFiles, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
